package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.dto.CnxjrjzDto;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.CnxjrjzMapper;
import com.ant.storage.pojo.Cnxjrjz;
import com.ant.storage.pojo.UserInfo;
import com.ant.storage.service.IAccountService;
import com.ant.storage.service.ICnxjrjzService;
import com.ant.storage.service.IDataMsgService;
import com.ant.storage.utils.commonUtil;
import com.ant.storage.vo.AccountingVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.List;
import java.util.stream.Collectors;

@Service
//@DS("four")
public class CnxjrjzServiceImpl extends ServiceImpl<CnxjrjzMapper, Cnxjrjz> implements ICnxjrjzService {

    @Autowired
    private IDataMsgService dataMsgService;
    @Resource
    IAccountService accountService;

    @Autowired
    public ServletContext servletContext;

    @Override
    public List<Cnxjrjz> getList(CnxjrjzDto dto) throws Exception {
        UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
        if(userInfo == null){
            throw new AntException(ResultCode.FAILURE.getCode(),"请登录");
        }
       // dataMsgService.changeDb(userInfo.getCityId());
        dataMsgService.changeDb(dto.getCityId());
        QueryWrapper<Cnxjrjz> wrapper=new QueryWrapper<>();
        if(dto.getFkmId()==null || ("").equals( dto.getFkmId())){
            throw new AntException(ResultCode.FAILURE.getCode(),"请输入科目id");
        }
        if(dto.getFyear()==null || ("").equals( dto.getFyear())){
            throw new AntException(ResultCode.FAILURE.getCode(),"请选择年份");
        }
        if(dto.getKeyword()==null || ("").equals( dto.getKeyword())){
            throw new AntException(ResultCode.FAILURE.getCode(),"请选择月份");
        }
        wrapper.eq("FPeriod",dto.getKeyword());
        wrapper.eq("FKmId",dto.getFkmId());
        wrapper.eq("FYear",dto.getFyear());
        wrapper.orderByAsc("FId");
        List<Cnxjrjz> list = this.baseMapper.selectList(wrapper);
        return list;
    }
    @Override
    public PageInfo<Cnxjrjz> pageList(CnxjrjzDto dto) {
        try {
            PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
            QueryWrapper<Cnxjrjz> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("FDate");
            PageInfo<Cnxjrjz> info = new PageInfo<>(this.baseMapper.selectList(wrapper));
            return info;
        } catch (Exception e) {
            throw new AntException(ResultCode.BODY_NOT_MATCH);
        }
    }

    @Override
    public void saveCnxjrjz(Cnxjrjz cnxjrjz) throws Exception {
        UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
        if(userInfo == null){
            throw new AntException(201,"请登录");
        }
        if(cnxjrjz.getCityId()==null || cnxjrjz.getCityId() == 0){
            throw new AntException(ResultCode.FAILURE.getCode(),"城市id无效");
        }
        //获取支付记录的类型
        List<Integer> list =getType(cnxjrjz.getCityId());
        if(list==null || list.size()==0){
            throw new AntException(ResultCode.FAILURE.getCode(),"没有数据");
        }
        //获取支付记录的类型
        List<AccountingVo> totalPrice = getTotalPrice(cnxjrjz.getCityId());
        //支付类型
        list.stream().forEach(item -> {
            try {
                Integer account=  selAccount(item);
                List<AccountingVo> accountingVoStream = totalPrice.stream().filter(vo -> vo.getType().equals(item))
                        .collect(Collectors.toList());
                if(accountingVoStream==null || accountingVoStream.size()==0){
                    throw new AntException(ResultCode.FAILURE.getCode(),"没有数据");
                }
                saveRjz(accountingVoStream,account,userInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    public void  saveRjz(List<AccountingVo> list,Integer type, UserInfo userInfo) throws Exception {
        dataMsgService.changeDb(100);
        list.stream().forEach(item -> {
            Cnxjrjz cnxjrjz1 = new Cnxjrjz();
            String fexp="订单号:"+item.getOrderNumber()+";营业额";
            cnxjrjz1.setFkmid(type);
            cnxjrjz1.setFyear(commonUtil.getYear());
            cnxjrjz1.setFperiod(commonUtil.getMonth());// 6 月
//            cnxjrjz1.setFyear(2018);
//            cnxjrjz1.setFperiod(6);// 6 月
            cnxjrjz1.setFdate(commonUtil.getYearMonthDay());
            cnxjrjz1.setFgroup(null);// 凭证字
            cnxjrjz1.setFnum(0);// 凭证号
            cnxjrjz1.setFexp(fexp);
            cnxjrjz1.setFaccountid2(null);
            cnxjrjz1.setFacctidside(null); //对方科目
            cnxjrjz1.setFdebit(item.getMoney().floatValue());
            cnxjrjz1.setFdebitb(item.getMoney().floatValue());
            cnxjrjz1.setFcredit(0F);
            cnxjrjz1.setFcreditb(0F);
            cnxjrjz1.setFrate(1f);
            cnxjrjz1.setFlyr(userInfo.getUserName());
            cnxjrjz1.setFsource(1);
            cnxjrjz1.setFremark(null);
            cnxjrjz1.setFvoucherid(-1);
            cnxjrjz1.setFgenvch(null);
            cnxjrjz1.setFvchyear(null);
            cnxjrjz1.setFvchperiod(null);
            cnxjrjz1.setFsourceid(null);
            cnxjrjz1.setFattachments(0);
            cnxjrjz1.setFid2(-1);
            cnxjrjz1.setFtablename(null);
            cnxjrjz1.setFitemdetailid(-1);
            cnxjrjz1.setFexchangeratetype(1);
            cnxjrjz1.setForderid(1);
            this.baseMapper.saveCnxjrjz(cnxjrjz1);
        });
    }

    public Integer selAccount(Integer id) throws Exception {
        dataMsgService.changeDb(100);
//        0: '通联支付',
//                1: '拉卡拉支付',
//                2: '抖音支付',
//                3: '美团小程序支付'
        String str="";
        //0 通联支付
        if(id==0){
            str="1012.02";
        }
        //1 拉卡拉支付
        if(id==1){
            str="1012.01";
        }
        //2 抖音支付
        if(id==2){
            str="1012.04";
        }
        //3 美团支付
        if(id==3){
            str="1012.03";
        }
        return this.baseMapper.selAccounts(str);
    }


    public  List<AccountingVo> getTotalPrice(Integer cityId) throws Exception {
        dataMsgService.changeDb(1001);
        String statTime=commonUtil.getYearMonthDay() +" 00:00:00";
        String endTime=commonUtil.getYearMonthDay() +" 23:59:59";
        // type 0 通联支付 1 拉卡拉支付 2抖音支付 3 美团支付
        // complete 0 完成 1 未完成
        List<AccountingVo> totalPrice = this.baseMapper.getTotalPrice(cityId,statTime, endTime);
        if(totalPrice==null || totalPrice.size()==0){
            throw new AntException(ResultCode.FAILURE.getCode(),"没有数据");
        }
        return totalPrice;
    }

    //获取支付记录的类型
    public  List<Integer> getType(Integer cityId) throws Exception {
        dataMsgService.changeDb(1001);
        String statTime=commonUtil.getYearMonthDay() +" 00:00:00";
        String endTime=commonUtil.getYearMonthDay() +" 23:59:59";
        // type 0 通联支付 1 拉卡拉支付 2抖音支付 3 美团支付
        // complete 0 完成 1 未完成
        List<Integer> list = this.baseMapper.getType(cityId, statTime, endTime);
        return list;
    }

    @Override
    public List<AccountingVo> getUrbanRevenue(Integer id) throws Exception {
        dataMsgService.changeDb(1001);
        String statTime=commonUtil.getYearMonthDay() +" 00:00:00";
        String endTime=commonUtil.getYearMonthDay() +" 23:59:59";
        return  this.baseMapper.getTotalPrice(id, statTime, endTime);
    }

    public static void main(String[] args) {
        System.out.println(commonUtil.getYearMonthDay());
    }

}
